#!/bin/bash

function install_nupkgs(){
  cd "$CID_WORKSPACE" || exit 1
  bm -action download -name union-sdk-nupkg -version 1.0.0 -release 143186 -token "$nupkg_token" -output ./
  bm -action download -type 3 -name Newtonsoft.Json -version 13.0.1 -file newtonsoft.json.13.0.1.nupkg -output .
  chmod 777 newtonsoft.json.13.0.1.nupkg
  mkdir package && mv newtonsoft.json.13.0.1.nupkg package && tar xf union-sdk-nupkg.tar -C package
  cp -pr Core/Core.csproj Core/Core.csproj.bak
  sed -i '/    <\/PropertyGroup>/i\        <RestoreSources>$(RestoreSources);..\/package;<\/RestoreSources>' Core/Core.csproj

  dotnet2 add Core package Newtonsoft.Json -v 13.0.1
  dotnet2 add Core package Microsoft.Extensions.DependencyInjection -v 3.1.5
  dotnet2 add Core package Microsoft.Extensions.Http -v 3.1.5
  dotnet2 add Core package Microsoft.Extensions.Http.Polly -v 3.1.5
  dotnet2 add Core package Microsoft.Extensions.Logging.Console -v 3.1.5
  dotnet2 add Core package Microsoft.Extensions.Logging.Debug -v 3.1.5

  mv Core/Core.csproj.bak Core/Core.csproj
  rm -rf union-sdk-nupkg.tar package
  rm -f ./*.zip ./*.tar
}

function init() {
  cd "$CID_WORKSPACE" || exit 1
  if [ -f "G42Cloud.sln" ]
  then
    rm -f G42Cloud.sln
  fi
  dotnet2 new sln -n G42Cloud
  dotnet2 sln add Core
}

function compile_core() {
  cd "$CID_WORKSPACE" || exit 1
  solution=$(find Core -name "*.sln")
  target_project_id=$(grep -E "^Project" "$solution" | grep "$service_name" | awk -F'"' '{print $8}')
  source_project_id=$(grep -E "^Project" G42Cloud.sln | grep "$service_name" | awk -F'"' '{print $8}')
  sed -i "s#${source_project_id}#${target_project_id}#" G42Cloud.sln
}

function compile_services() {
  cd "$CID_WORKSPACE" || exit 1
  for each in $(find Services -maxdepth 1 -mindepth 1 -type d)
  do
    service_name=${each##Services/}
    solution=$(find "$each" -name "*.sln")
    if [ "$solution" != "" ]
    then
      dotnet2 sln add "$each"
      target_project_id=$(grep -E "^Project" "$solution" | grep "$service_name" | awk -F'"' '{print $8}')
      source_project_id=$(grep -E "^Project" G42Cloud.sln | grep "$service_name" | awk -F'"' '{print $8}')
      sed -i "s#${source_project_id}#${target_project_id}#" G42Cloud.sln
    fi
  done
}

function pack_src() {
  cd "$CID_WORKSPACE" || exit 1
  folders=$(ls | grep -E -v ".cid|.git|.codecheck|buildscript|*.sh")
  tar czf g42cloud-sdk-net-src.tar.gz $(echo $folders)
}

function pack_bin() {
  version=$(cat "$CID_WORKSPACE"/VERSION)
  cd "$CID_WORKSPACE" || exit 1
  echo "Dotnet build"
  dotnet2 build -c Release || exit 3
  echo "Dotnet pack"
  dotnet2 pack --no-build -c Release /p:PackageVersion="$version" || exit 4

  mkdir nupkgs
  find ./ -name "*.nupkg" | xargs -I '{}' cp -f '{}' nupkgs
  tar czf g42cloud-sdk-net-bin.tar.gz nupkgs
}

function pack() {
  pack_src
  pack_bin

  cd "$CID_WORKSPACE" || exit 1
  version_suffix="$(cat "./VERSION").${CID_BUILD_TIME}.${CID_BUILD_NUMBER}"
  tar czf g42cloud-sdk-net-release."${version_suffix}".tar.gz g42cloud-sdk-net-src.tar.gz g42cloud-sdk-net-bin.tar.gz
  rm -rf g42cloud-sdk-net-src.tar.gz g42cloud-sdk-net-bin.tar.gz
}

function main() {
  install_nupkgs
  init
  compile_core
  compile_services
  pack
}

main "$*"
